package scau.markmapscau;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import scau.markmapscau.Model.Node.NodeArranger;
import scau.markmapscau.Model.Tool.ToolArranger;
import scau.markmapscau.Model.Tree.TreeArranger;
import scau.markmapscau.View.ViewerArranger;


public class LaunchMain extends Application {
    static public ViewerArranger viewerArranger;//前端页面布局者
    static public NodeArranger nodeArranger;
    static public TreeArranger treeArranger;
    static public ToolArranger toolArranger;

    static public ColorPicker colorPicker;
    static public ColorPicker colorPicker2;
    static public AnchorPane mindMarkMap;
    static public ScrollPane scrollPane;
    static public AnchorPane toolPane;
    //    static public Label scaleRate;
    static public Button add;
    static public Button delete;
    static public Pane struct;//结构
    static public AnchorPane smallPane;//缩略图
    static public ChoiceBox choiceStyleBox;
    static public Label title;

    public Stage stage;

    static public void initArranger() throws Exception{
        nodeArranger = new NodeArranger();
        treeArranger = new TreeArranger();
        toolArranger = new ToolArranger();
    }

    static public void initViewer(Stage stage){
        viewerArranger = new ViewerArranger(nodeArranger,treeArranger,toolArranger,mindMarkMap,scrollPane,struct,smallPane,add,delete,choiceStyleBox,toolPane,title,colorPicker2,stage);
    }

    @Override
    public void start(Stage stage) throws Exception {
        initArranger();//1.初始化管理者
        FXMLLoader fxmlLoader = new FXMLLoader(LaunchMain.class.getResource("scau-markmap.fxml"));
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root, 1200, 700);
        scene.getStylesheets().add(LaunchMain.class.getResource("scau-markmap.css").toExternalForm());//添加css文件
        stage.setTitle("思维导图");
        stage.setScene(scene);
        stage.show();
        mindMarkMap = (AnchorPane) root.lookup("#MindMarkZone");
        scrollPane = (ScrollPane)root.lookup("#MindMapScrollPane") ;
        struct =(Pane)root.lookup("#StructZone");//结构区
        smallPane = (AnchorPane) root.lookup("#SmallPane");//缩略图区
        toolPane=(AnchorPane)root.lookup("#ToolPane") ;
        colorPicker=(ColorPicker)root.lookup("#ColorPick") ;
        delete=(Button)root.lookup("#delete") ;
        add=(Button)root.lookup("#add");
        colorPicker2=(ColorPicker)root.lookup("#ColorPick2");
        choiceStyleBox = (ChoiceBox) root.lookup("#choiceStyle");
        title = (Label)root.lookup("#title");


        initViewer(stage);

        //背景颜色选择器
        colorPicker.setOnAction(a->{
            Color color=colorPicker.getValue();
            String backColor = Integer.toHexString(color.hashCode());
//            smallPane.setStyle(backColor);
            nodeArranger.setBackColor(backColor);
            mindMarkMap.setBackground(new Background(new BackgroundFill(color,null,null)));
            scrollPane.setBackground(new Background(new BackgroundFill(color,null,null)));
            smallPane.setBackground(new Background(new BackgroundFill(color,null,null)));
            System.out.println("颜色"+color);
            viewerArranger.drawSmallPane();
        });

        //节点颜色选择器
        colorPicker2.setOnAction(a->{
            if(!checkForEmptyList()){return ;}
            if(!checkForChooseNode()){return;}
            Color color=colorPicker2.getValue();
            String hex1 = Integer.toHexString(color.hashCode());
            //选中的颜色赋值给节点的颜色属性
            viewerArranger.nodeArranger.getNodeList().get(viewerArranger.selectNum).setColor("#"+hex1);
            viewerArranger.drawNodesAndLines(viewerArranger.nodeArranger.getNodeList());
        });


        //字体选择器
        String[] names = {"FangSong","HeiTi", "KaiTi", "FangSong_GB2312", "KaiTi_GB2312", "Microsoft JhengHei", "Microsoft YaHei"};
        ChoiceBox choiceBox=new ChoiceBox(FXCollections.observableArrayList("仿宋","黑体","楷体","仿宋_GB2312", "楷体_GB2312", "微軟正黑體", "微软雅黑体"));
        choiceBox.setValue("仿宋");
        choiceBox.setLayoutY(30);
        choiceBox.setLayoutX(800);
        choiceBox.setStyle("-fx-background-color: #EDF1F2");
        toolPane.getChildren().add(choiceBox);
        choiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
                if(!checkForEmptyList()){return;}
                if(!checkForChooseNode()){return;}
                viewerArranger.nodeArranger.getNodeList().get(viewerArranger.selectNum).setStyle(names[t1.intValue()]);//修改选中的框的字体样式
                viewerArranger.drawNodesAndLines(viewerArranger.nodeArranger.getNodeList());//画图
            }
        });

        mindMarkMap.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if(mouseEvent.getButton() == MouseButton.PRIMARY){

                    viewerArranger.selectNum = -1;
                    viewerArranger.drawNodesAndLines(viewerArranger.nodeArranger.getNodeList());
                }
            }
        });
    }

    void callAlertForEmptyList(){
        Alert alert = new Alert(Alert.AlertType.WARNING,"请先新建思维导图再操作！");
        alert.showAndWait();
    }

    boolean checkForEmptyList(){
        ViewerArranger viewerArranger = LaunchMain.viewerArranger;
        if(viewerArranger.nodeArranger.getNodeList() == null){
            callAlertForEmptyList();
            return false;
        }
        if(viewerArranger.nodeArranger.getNodeList().size() == 0){
            callAlertForEmptyList();
            return false;
        }
        return true;
    }

    void callAlertForMutCenter(){
        Alert alert = new Alert(Alert.AlertType.ERROR,"你不能创建多个中心节点!");
        alert.showAndWait();
    }

    boolean checkForMutCenter(){
        ViewerArranger viewerArranger  = LaunchMain.viewerArranger;
        if(viewerArranger.nodeArranger.getNodeList().size()!=0){//防止多次创建中心节点
            callAlertForMutCenter();
            return false;
        }
        return true;
    }

    void callAlertForNotChooseNode(){
        Alert alert = new Alert(Alert.AlertType.ERROR,"你没有选择任何节点!");
        alert.showAndWait();
    }

    boolean checkForChooseNode(){
        ViewerArranger viewerArranger = LaunchMain.viewerArranger;
        if(viewerArranger.selectNum == -1){
            callAlertForNotChooseNode();
            return false;
        }
        return true;
    }


    //Launch函数
    public static void main(String[] args) {
        launch();
    }
}